package cc.mrbird.system.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.AjaxResponse;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.system.domain.ProblemFeedback;
import cc.mrbird.system.service.ProblemFeedbackService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import java.util.Map;
import java.util.Optional;


@Controller
public class ProblemFeedbackController  extends BaseController {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ProblemFeedbackService problemFeedbackService;


    @Log("获取问题反馈信息")
    @RequestMapping("problemFeedback")
    @RequiresPermissions("problemFeedback:list")
    public String index() {
        return "system/problemFeedback/problemFeedback";
    }


    /**
     * 获取问题反馈信息
     * @return
     */
    @RequestMapping("problemFeedback/list")
    @RequiresPermissions("problemFeedback:list")
    @ResponseBody
    public Map<String, Object> problemFeedbackList(QueryRequest request, ProblemFeedback problemFeedback) {
        return super.selectByPageNumSize(request, () -> this.problemFeedbackService.findAllProblemFeedback(problemFeedback, request));
    }

    @Log("获取单个反馈信息")
    @RequestMapping("problemFeedback/get")
    @ResponseBody
    public ResponseBo getProblemFeedback(@RequestParam("id") Integer id) {
        try {
            ProblemFeedback data = this.problemFeedbackService.findProblemFeedback(id);
            return ResponseBo.ok(data);
        } catch (Exception e) {
            log.error("获取反馈信息失败", e);
            return ResponseBo.error("获取项反馈信息，请联系网站管理员！");
        }
    }

    @ResponseBody
    @RequestMapping("problemFeedback/upload")
    public AjaxResponse upload (@RequestParam("file")MultipartFile files) throws IOException {

        ProblemFeedback upload = problemFeedbackService.upload(files);
        if (ObjectUtils.anyNotNull(upload)) {
            return new AjaxResponse(true,"上传成功",upload);
        }
        return new AjaxResponse(false,"上传失败",new ProblemFeedback());
    }


    @Log("新增问题反馈信息")
    @RequiresPermissions("problemFeedback:add")
    @RequestMapping("problemFeedback/add")
    @ResponseBody
    public ResponseBo addProblemFeedback(ProblemFeedback problemFeedback) {
        try {
            this.problemFeedbackService.addProblemFeedback(problemFeedback);
            return ResponseBo.ok("新增问题反馈信息成功！");
        } catch (Exception e) {
            log.error("新增问题反馈信息失败", e);
            return ResponseBo.error("新增问题反馈信息失败，请联系网站管理员！");
        }
    }

    @Log("修改反馈信息")
    @RequiresPermissions("problemFeedback:update")
    @RequestMapping("problemFeedback/update")
    @ResponseBody
    public ResponseBo updateProblemFeedback(ProblemFeedback problemFeedback) {
        try {
            this.problemFeedbackService.updateProblemFeedback(problemFeedback);
            return ResponseBo.ok("修改反馈信息成功！");
        } catch (Exception e) {
            log.error("修改反馈信息失败", e);
            return ResponseBo.error("修改反馈信息失败，请联系网站管理员！");
        }
    }

    @Log("删除反馈信息")
    @RequiresPermissions("problemFeedback:delete")
    @RequestMapping("problemFeedback/delete")
    @ResponseBody
    public ResponseBo deleteProjectEntry(String ids) {
        try {
            this.problemFeedbackService.deleteProblemFeedback(ids);
            return ResponseBo.ok("删除反馈信息成功！");
        } catch (Exception e) {
            log.error("删除反馈信息失败", e);
            return ResponseBo.error("删除反馈信息失败，请联系网站管理员！");
        }
    }

    /**
     * 附件下载
     * @param
     * @param request
     * @param response
     */
    @RequestMapping(value = "problemFeedback/downloadFile")
    public void downloadFile(@RequestParam("id") Integer id, HttpServletRequest request, HttpServletResponse response) {
        BufferedInputStream in = null;
        BufferedOutputStream out = null;
        FileInputStream fis=null;
        try {
            /*根据id单条查询*/
            ProblemFeedback problemFeedback = problemFeedbackService.findProblemFeedback(id);
            response.setContentType("text/html;charset=UTF-8");
            //String rootpath = request.getSession().getServletContext().getRealPath("/");
            String rootpath = "D:/file";//配置文件中指定下载文件路径。例D:\files
            File f = new File(rootpath + File.separator+Optional.ofNullable(problemFeedback.getEnclosure()).orElse(""));
            response.reset();
            response.setContentType("application/x-excel");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename="
                    + new String(Optional.ofNullable(problemFeedback.getEnclosure()).orElse("").getBytes("gbk"),"iso-8859-1"));
            response.setHeader("Content-Length",String.valueOf(f.length()));
            fis=new FileInputStream(f);
            in = new BufferedInputStream(fis);
            out = new BufferedOutputStream(response.getOutputStream());
            byte[] data = new byte[1024];
            int len = 0;
            while (-1 != (len=in.read(data, 0, data.length))) {
                out.write(data, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                }
            }
        }
    }




}
